package com.cf.core.stage;


import com.cf.Event;
import com.cf.EventBus;
import com.cf.EventHandler;
import com.cf.timer.HeartBeatTask;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * game-server-Checkpoint
 * 关卡
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/4/27 下午 05:24
 */
@Slf4j
public abstract class Stage implements EventBus, EventHandler {

    public volatile boolean end = false;

    private String stageNum;

    private int stageTime = 0;

    public volatile int stageLeftTime = 0;

    public int reduceAndGetLeftTime() {
        --stageLeftTime;
        log.info("当前回合剩余【{}】", stageLeftTime);
        return stageLeftTime;
    }

    private List<EventHandler> handlers = new ArrayList();

    public Stage(int stageTime, String stageNum) {
        if (stageTime <= 0)
            throw new IllegalArgumentException("参数值不能>=0");
        this.stageNum = stageNum;
        this.stageTime = stageTime;
        this.stageLeftTime = stageTime;
        this.registerEventHandler(this);
    }

    @Override
    public void broadcastEvent(final Event event) {
        handlers.forEach(handler -> handler.handle(event));
    }

    @Override
    public void registerEventHandler(EventHandler handler) {
        if (handler != null)
            handlers.add(handler);
    }

    public void end() {
        this.end = true;
        clean();
    }


    protected abstract void clean();

    public String getStageNum() {
        return stageNum;
    }

    public abstract String getStatue();

    public int getStageTime() {
        return stageTime;
    }

    public int getStageLeftTime() {
        return stageLeftTime;
    }
}
